package com.xdjk.networkdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.xdjk.networkdemo.R;
import com.xdjk.networkdemo.listener.OnItemClickLitener;
import com.xdjk.networkdemo.listener.OnLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 交易列表adapter
 */

public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<String> transList;
    private LayoutInflater mInflater;

    private OnItemClickLitener onItemClickLitener;
    private OnLoadMoreListener onLoadMoreListener;

    public ListAdapter(Context context, List<String> posList) {
        this.mContext = context;
        this.transList = posList;
        mInflater = LayoutInflater.from(context);
    }

    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener) {
        this.onItemClickLitener = onItemClickLitener;
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = mInflater.inflate(R.layout.list_item, viewGroup, false);
        RecyclerView.ViewHolder holder = new ContentItemViewHolder(view);
        return holder;
    }


    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (position < transList.size()) {
            String listBean = transList.get(position);
            if (null != transList) {
                ((ContentItemViewHolder) holder).tvItem.setText(listBean);
                ((ContentItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != onItemClickLitener) {
                            onItemClickLitener.onItemClick(v, position);
                        }
                    }
                });
            }
        }
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return transList.size();
    }

    class ContentItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_item)
        TextView tvItem;
        public ContentItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

    }
}
